import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;


public class BookSet implements Set {

	ArrayList store= new ArrayList();
	ArrayList count= new ArrayList();
	
	BookSet(){
	}

	@Override
	public boolean add(Object e) {
		if(e instanceof Comparable){
		Iterator i = this.iterator();
		int c = 0;
		Object ob;
		Comparable cur;
		Comparable eh = (Comparable) e;
		if(store.contains(e)){
			c=store.indexOf(e);
			if (count.get(c) instanceof Integer){
				int u =  (Integer)count.get(c);
				count.remove(c);
				count.add(c,u+1);
				return true;
			}
		}	
		while(i.hasNext()){
			ob = i.next();
			if (ob instanceof Comparable){
				cur = (Comparable) ob;
				if (eh.compareTo(cur) > 0)
					c++;
			}
		}
		store.add(c,e);
		count.add(c,1);
		return true;
		}
		count.add(0);
		return store.add(e);
	}

	public boolean addAll(Collection c) {
		Iterator i = c.iterator();
		boolean b = false;
		while (i.hasNext())
			b |= add(i.next());
		return b;
	}

	public void clear() {
		store.clear();
		count.clear();
	}

	@Override
	public boolean contains(Object o) {
		
		return store.contains(o);
	}

	@Override
	public boolean containsAll(Collection c) {
		Iterator i = c.iterator();
		boolean b = false;
		while(i.hasNext())
			b |= contains(i.next());
		return b;
	}

	@Override
	public boolean isEmpty() {
		return store.isEmpty();
	}

	@Override
	public Iterator iterator() {
		return new Iterator(){
			int i = 0;
			public boolean hasNext() {
				return i < store.size();
			}

			@Override
			public Object next() {
				if (hasNext()){
					i++;
					return store.get(i-1);
				}
				return null;
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException("");
			}
			
		};
	}

	@Override
	public boolean remove(Object o) {
		if(store.contains(o)){
			count.remove(store.indexOf(o));
			store.remove(o);
			return true;
		}
		return false;
	}

	@Override
	public boolean removeAll(Collection c) {
		Iterator i = c.iterator();
		boolean b = false;
		while(i.hasNext())
			b |= remove(i.next());
		return b;
	}

	@Override
	public boolean retainAll(Collection c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int size() {
		Object c;
		int u=0;
		Iterator i = count.iterator();
		while(i.hasNext()){
			c = i.next();
			if (c instanceof Integer)
				u += (Integer)c;
		}
			
		return u;
	}

	@Override
	public Object[] toArray() {
		return store.toArray();
	}


	@Override
	public Object[] toArray(Object[] a) {
		throw new UnsupportedOperationException() ;
	}



}
